package top.chatgqt.SmartChargeStation.demo.netty.handlers;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

/*
服务端，心跳超时处理器
 */
@Slf4j
public class ImoocServerHeartBeatHandler extends ChannelInboundHandlerAdapter {

    //
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        /*
        因为userEventTriggered接收了IdleStateHandler传递过来的IdleStateEvent事件
        所以Object evt可以强制转换为IdleStateEvent事件
         */

        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;

            switch (event.state()) {
                case READER_IDLE:
                    ctx.channel().close();
                    break;
                case WRITER_IDLE:
                    break;
                case ALL_IDLE:
                    break;
            }
        }
    }
}
